package com.yjzx.care.base;

import com.yjzx.care.*;

/**
 * @author yjzx
 * @description ElementEnum
 * @since 2025/8/5
 */
public enum ElementFactoryEnum {
    /**
     * 函数式无返if
     */
    NO_RETURN_TYPE_CLASS("函数式无返if", ElementNoReturn.class),
    /**
     * 函数式有返if
     */
    RETURN_TYPE_CLASS("函数式有返if", ElementReturn.class),
    /**
     * 函数式for循环
     */
    LOOP_FOR_TYPE_CLASS("函数式for循环", LoopFor.class),
    /**
     * 函数式while循环
     */
    LOOP_WHILE_TYPE_CLASS("函数式while循环", LoopWhile.class),
    /**
     * 函数式do...while循环
     */
    LOOP_DO_WHILE_TYPE_CLASS("函数式do...while循环", LoopDoWhile.class),
    /**
     * 函数式无返builder if
     */
    NO_RETURN_BUILDER_TYPE_CLASS("函数式无返builder if", ElementNoReturn.ElementNoReturnBuilder.class),
    /**
     * 函数式有返builder if
     */
    RETURN_BUILDER_TYPE_CLASS("函数式有返builder if", ElementReturn.ElementReturnBuilder.class),
    /**
     * 函数式builder for循环
     */
    LOOP_FOR_BUILDER_TYPE_CLASS("函数式builder for循环", LoopFor.LoopForBuilder.class),
    /**
     * 函数式builder while循环
     */
    LOOP_WHILE_BUILDER_TYPE_CLASS("函数式builder while循环", LoopWhile.LoopWhileBuilder.class),
    /**
     * 函数式builder do...while循环
     */
    LOOP_DO_WHILE_BUILDER_TYPE_CLASS("函数式builder do...while循环", LoopDoWhile.LoopDoWhileBuilder.class);

    private final String name;
    private final Class<?> clazz;

    ElementFactoryEnum(String name, Class<? extends MyInterface> clazz) {
        this.name = name;
        this.clazz = clazz;
    }

    /**
     * 创建MyInterface接口实例，也可使用MyInterface.newInstance()创建
     *
     * @param <T> 需要类型
     * @return 接口实例
     */
    public <T extends MyInterface> T elementNewInstance() {
        T t;
        try {
            t = (T) MyInterface.toConstructorMethodHandle(clazz).invoke();
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
        return t;
    }
}
